/* 
 * --------------------------------------------------------------------
 * This is the android single touch handler class that implements touch 
 * events listener.  
 * 2011, by Wu Mu Xian
 * --------------------------------------------------------------------
*/

package mygame.myandroidgameframework;

import java.util.ArrayList;
import java.util.List;

import android.view.MotionEvent;
import android.view.View;

import mygame.basicgameframework.Pool;
import mygame.basicgameframework.Input.TouchEvent;
import mygame.basicgameframework.Pool.PoolObjectFactory;

public class MyAndroidSingleTouchHandler implements MyAndroidTouchHandler {
	
	boolean isTouched; // if the screen is touched
	int touchX; // the x-coordinate of the touch point
	int touchY; // the y-coordinate of the touch point
	Pool<TouchEvent> touchEventPool; // the touch events pool
	List<TouchEvent> touchEvents = new ArrayList<TouchEvent>(); // the touch events
	// the touch events buffer
	List<TouchEvent> touchEventsBuffer = new ArrayList<TouchEvent>();
	float scaleX;
	float scaleY;

	// the constructor
	public MyAndroidSingleTouchHandler(View view, float scaleX, float scaleY) { 
        PoolObjectFactory<TouchEvent> factory = new PoolObjectFactory<TouchEvent>() { 
            public TouchEvent createObject() { 
                return new TouchEvent(); 
            }             
        }; 
        touchEventPool = new Pool<TouchEvent>(factory, 100); 
        view.setOnTouchListener(this); 
 
        this.scaleX = scaleX; 
        this.scaleY = scaleY; 
    }
	
	// called when the screen is touched
	public boolean onTouch(View v, MotionEvent event) {
		synchronized(this) { 
            TouchEvent touchEvent = touchEventPool.newObject(); 
            switch (event.getAction()) { 
            case MotionEvent.ACTION_DOWN : 
                touchEvent.type = TouchEvent.TOUCH_DOWN ; 
                isTouched = true; 
                break; 
            case MotionEvent.ACTION_MOVE : 
                touchEvent.type = TouchEvent.TOUCH_DRAGGED ; 
                isTouched = true; 
                break; 
            case MotionEvent.ACTION_CANCEL :                 
            case MotionEvent.ACTION_UP : 
                touchEvent.type = TouchEvent.TOUCH_UP ; 
                isTouched = false; 
                break; 
            } 
             
            touchEvent.x = touchX = (int)(event.getX() * scaleX); 
            touchEvent.y = touchY = (int)(event.getY() * scaleY); 
            touchEventsBuffer.add(touchEvent);                         
             
            return true; 
        }
	}

	// check the screen state
	public boolean isTouchDown(int pointer) {
		synchronized(this) { 
            if(pointer == 0) 
                return isTouched; 
            else 
                return false; 
        }
	}

	// get the x-coordinate of the touched point
	public int getTouchX(int pointer) {
		synchronized(this) { 
            return touchX; 
        }
	}

	// get the y-coordinate of the touched point
	public int getTouchY(int pointer) {
		synchronized(this) { 
            return touchY; 
        } 
	}

	// get the touch events
	public List<TouchEvent> getTouchEvents() {
		synchronized(this) {      
            int len = touchEvents.size(); 
            for( int i = 0; i < len; i++ ) 
                touchEventPool.free(touchEvents.get(i)); 
            touchEvents.clear(); 
            touchEvents.addAll(touchEventsBuffer); 
            touchEventsBuffer.clear(); 
            return touchEvents; 
        } 
	}

}
